/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components.focusManager;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;

/**
 *
 * @author glauco
 */
public class FocusManagers {

    /**
     * Lista de FocusManagers
     */
    List<JPanelFocusManager> managers;

    private FocusManagers() {
        managers = new ArrayList<JPanelFocusManager>();
    }

    /**
     * Pega a instancia.
     */
    public static FocusManagers getInstance() {
        return FocusManagersHolder.INSTANCE;
    }

    /**
     * Adiciona um FocusManager
     * @param manager the manager of focus.
     */
    public void addFocusManager(JPanelFocusManager manager){
        managers.add(manager);
    }

    /**
     * Busca um FocusManager a partir do componente.
     * É retornado o FocusManager Pai do componente, se não encontrado nenhum
     * FocusManager Pai do componente recebido por parametro é retornado null.
     * @param component componente Filho de um FocusManager.
     * @return the FocusManager or null.
     */
    public JPanelFocusManager getFocusManager(JComponent component){

        for (JPanelFocusManager manager : managers) {
            if(manager.isAncestorOf(component))
                return manager;
        }

        return null;
    }

    /**
     * Remove um FocusManager
     * @param manager the manager of focus.
     * @return se o elemento foi excluido da lista.
     */
    public boolean removeFocusManager(JPanelFocusManager manager){
        return managers.remove(manager);
    }

    /**
     * Holder
     */
    private static class FocusManagersHolder {
        private static final FocusManagers INSTANCE = new FocusManagers();
    }
 }
